<?
$admin_area=1;
include "../includes/common_admin_inc.php";

echo "<form method='post' action='make_sql.php'>";
echo "<b>DB-Version:</b><br><input type='text' name='db_version' value='".getParam("db_version","")."'><br><br>";
echo "<b>Custom SQL before:</b><br><textarea cols='50' rows='10' name='db_sql_pre'>".ereg_replace("\\\&#039;","'",getParam("db_sql_pre",""))."</textarea><br><br>";
echo "<b>Custom SQL after: </b><br><textarea cols='50' rows='10' name='db_sql_aft'>".ereg_replace("\\\&#039;","'",getParam("db_sql_aft",""))."</textarea><br><br>";
echo "<input type='submit' name='submit' value='Generate'><br>";
echo "</form>";

if (getParam("submit",""))
	buildFile();




function buildFile()
{
	global $set_mysql_base;
	$lTableArray=getTables();
	$lStr="";
	
	$lPre=ereg_replace("\\\&#039;","'",getParam("db_sql_pre",""));
	$lAft=ereg_replace("\\\&#039;","'",getParam("db_sql_aft",""));
	$lDb=getParam("db_version","");
	$lTime=time();
	$lStr=$lDb . ":$lTime\n<br>";
	$lStr.=$lPre . "\n<br>";	
	foreach ($lTableArray as $lTableItem)
	{
		
		$lPrimaryKeyField=getIndex($lTableItem);
		
		$res=q("show fields from $lTableItem from $set_mysql_base");
		while ($row=mysql_fetch_array($res))
		{
			$lField = $row['Field'];
			$lType = $row['Type'];
			$lKey = $row['Key'];
			$lExtra = $row['Extra'];
			
			if (!eregi("^e_",$lField))
			{
		
				$lStr.="$lTableItem|";
				if ($lField==$lPrimaryKeyField)
				{	
					$lStr.="P";	
					
				}
				$lStr.="|$lField|$lType|$lExtra\n<br>";
			}
		}
		
	}
	
	//$lStr.=$lAft . "\n<br>";	
	echo $lStr;
	$lStr=ereg_replace("<br>","",$lStr);
	$fp=fopen("setup/db_up.txt","w+");
	fwrite($fp,$lStr);
}
?>